package miscellaneousTests;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;
/**
 * Class that can be run to test inserting a timestamp into a database
 * @author Vance
 *
 */
public class testTime {
	private static Properties databaseInfo;
	private static String url;
	/**
	 * try to connect the the database according to the databaseInfo properties file,
	 * except that the schema will be "test" in order not to mess up with the 
	 * working schema.
	 * @return
	 */
	private static Statement initializeDatabase(){
		try{
			//Initialize properties. 
			databaseInfo = new Properties();
			databaseInfo.load(new FileInputStream
					("properties/databaseInfo.properties"));
		}catch (FileNotFoundException e){
			e.printStackTrace();
			System.err.print("\nProperties file for creating database + " +
					"not found\n");
		}catch (IOException e){
			e.printStackTrace();
			System.err.print("\nProperties file for creating database + " +
					"not found\n");
		}

		try {
			//Register the driver class
			Class.forName(databaseInfo.getProperty("DatabaseDriverClass"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.err.println("You need to install the specified database driver " +
			"before running this software.");
			System.exit(1);
		}
		
		//For example, url looks like "jdbc:mysql://localhost:3306/mysql"
		
		Connection con;
		try {//Try to connect to the database using the provided information 
			//With the schema set to what is provided. Let's say it's "bluesky"
			url = "jdbc:mysql://"+databaseInfo.getProperty("DatabaseAddress")
				+":"+databaseInfo.getProperty("DatabasePort")+"/test";
			con = (Connection) DriverManager.getConnection(url,
					databaseInfo.getProperty("User"), 
					databaseInfo.getProperty("Password"));
	
		}catch (SQLException d){
			//Now it really goes wrong. Exit the system.
			d.printStackTrace();
			return null;
			
		}
		System.out.println("Database Connection established.");
		System.out.println("URL: " + url);
		System.out.println("Connection: " + con);
		//Try to initialize the statement
		try {
			return (Statement) con.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

		
	}
	
	public static void main(String[]args){
		Date timeNow = new Date();
		Timestamp ts =  new Timestamp(timeNow.getTime());
		
		System.out.println("time now is (for Date):"+timeNow);
		System.out.println("time now is (for Timestamp):"+ts);
		Statement stat = initializeDatabase();
		String query = "INSERT INTO battery (Timestamp) VALUES('"+ts+"')";
		System.out.println(query);
		try {
			stat.executeUpdate(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
